﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using dhDataAcess.baseBLL;
using dhDataAcess.orderLinqDAL;
using dhDataAcess.dataAcess.produceDataSetTableAdapters;
using dhDataAcess.dataAcess;
using System.Data.SqlClient;
using dhDataAcess.orderDAL;
using dhDataAcess.produceLinq;
using dhDataAcess.produceDAL;
using dhDataAcess.sysDAL;
using dhDataAcess.dataAcess.ViewDataSetTableAdapters;

namespace dhSystemBLL.produceBLL
{
   public  class orderStockOutBLL
    {

       private V_orderStockOutTableAdapter _V_orderStockOutAdapter = null;
       private V_orderStockOutTableAdapter V_orderStockOutAdapter
        {
            get
            {
                if (_V_orderStockOutAdapter == null) _V_orderStockOutAdapter = new V_orderStockOutTableAdapter();
                return _V_orderStockOutAdapter;

            }
        }
       private orderStockOutMasterTableAdapter _orderStockOutMasterlAdapter = null;
       private orderStockOutMasterTableAdapter orderStockOutMasterAdapter
        {
            get
            {
                if (_orderStockOutMasterlAdapter == null) _orderStockOutMasterlAdapter = new orderStockOutMasterTableAdapter();
                return _orderStockOutMasterlAdapter;

            }
        }
       private orderStockOutDetailTableAdapter _orderStockOutDetailAdapter = null;
       private orderStockOutDetailTableAdapter orderStockOutDetailAdapter
        {
            get
            {
                if (_orderStockOutDetailAdapter == null) _orderStockOutDetailAdapter = new orderStockOutDetailTableAdapter();
                return _orderStockOutDetailAdapter;

            }
        }
        /// <summary>
       /// 查询订单成品出库主表
        /// </summary>
        /// <returns></returns>

       public produceDataSet.orderStockOutMasterDataTable getorderStockOutMasterAll()
        {
            try
            {
                return orderStockOutMasterAdapter.GetDataorderStockOutMasterALL();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
       /// 查询订单成品出库从表
        /// </summary>
        /// <returns></returns>

       public produceDataSet.orderStockOutDetailDataTable getorderStockOutDetailAll()
        {
            try
            {
                return orderStockOutDetailAdapter.GetDataorderStockOutDetailALL();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
       /// 根据入库单号查询订单成品出库表
        /// </summary>
        /// <param name="groupNum"></param>
        /// <returns></returns>
       public produceDataSet.orderStockOutMasterDataTable getorderStockOutMasterAllBybillNo(string billNo)
        {
            try
            {
                return orderStockOutMasterAdapter.GetDataorderStockOutMasterBybillNo(billNo);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
       /// <summary>
       /// 根据入库单号查询视图
       /// </summary>
       /// <param name="groupNum"></param>
       /// <returns></returns>
       public ViewDataSet.V_orderStockOutDataTable getV_orderStockOutAllBybillNo(string billNo)
       {
           try
           {
               return V_orderStockOutAdapter.GetDataV_orderStockOutAllBybillNo (billNo);
           }
           catch (Exception ex)
           {
               throw new Exception(ex.Message.ToString());
           }
       }
       /// <summary>
       /// 根据入库单号查询视图
       /// </summary>
       /// <param name="groupNum"></param>
       /// <returns></returns>
       public ViewDataSet.V_orderStockOutDataTable getV_orderStockOutALL()
       {
           try
           {
               return V_orderStockOutAdapter.GetDataV_orderStockOutAll ();
           }
           catch (Exception ex)
           {
               throw new Exception(ex.Message.ToString());
           }
       }
        /// <summary>
       /// 根据入库单号查询订单成品出库从表
        /// </summary>
        /// <param name="groupNum"></param>
        /// <returns></returns>
       public produceDataSet.orderStockOutDetailDataTable getsorderStockOutDetailAllBybillNo(string billNo)
        {
            try
            {
                return orderStockOutDetailAdapter.GetDataorderStockOutDetailBybillo (billNo);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
       /// 修改订单成品出库表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
       public int orderStockOutInsert(produceDataSet .orderStockOutMasterDataTable masterTable, produceDataSet.orderStockOutDetailDataTable pTable, string state, string billNo, string Date)
        {
            orderStockOutDetailModel orderStockOutDetailRow = new orderStockOutDetailModel();
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                orderStockOutMasterDAL orderStockOutMaster = new orderStockOutMasterDAL(tn, conn);
                orderStockOutDetailDAL orderStockOutDetail = new orderStockOutDetailDAL(tn, conn);
                orderStockOutMaster.orderStockOutMasterII  (masterTable, pTable);
                //basListDAL basList = new basListDAL(tn, conn);
                //if (state == "新增")
                //{

                //    produceDataSet.orderStockOutMasterDataTable orderStockOutTable = getorderStockOutMasterAllBybillNo(billNo);
                //    if (orderStockOutTable.Rows.Count == 0)
                //    {
                //        orderStockOutMaster.orderStockOutMasterInsert(row);
                //    }
                //    else
                //    {
                //        row.billNo = "S" + Date + "-" + basicSql.getOrderSerail("订单成品出库单号");
                //        orderStockOutMaster.orderStockOutMasterInsert(row);
                //    }

                //    //更新单据编号的过程 
                //    string orderSerial = basicSql.getOrderSerail("订单成品出库单号");
                //    basList.basListretbasNumUpdate((Convert.ToInt32(orderSerial) + 1).ToString(), "订单成品出库单号");
                //}
                //if (state == "编辑")
                //{
                //    orderStockOutMaster.orderStockOutMasterUpdate(row);
                //}


                //foreach (produceDataSet.orderStockOutDetailRow detailRow in pTable)
                //{
                //    if (detailRow.RowState != DataRowState.Deleted)
                //    {
                //        orderStockOutDetailRow.billNo = detailRow.billNo;
                //        orderStockOutDetailRow.orderDetailNo = detailRow.orderDetailNo;
                //        orderStockOutDetailRow.pName = detailRow.pName;
                //        orderStockOutDetailRow.qty = detailRow.qty;
                //        orderStockOutDetailRow.unit = detailRow.unit;
                //        orderStockOutDetailRow.boxno = detailRow.boxno;
                //        orderStockOutDetailRow.boxList = detailRow.boxList;
                //        orderStockOutDetailRow.weightJ = detailRow.weightJ;
                //        orderStockOutDetailRow.weightM = detailRow.weightM;
                //        orderStockOutDetailRow.createDate = DateTime.Now;
                        
                //        if (!detailRow.IsmarkNull())
                //        {
                //            orderStockOutDetailRow.mark = orderStockOutDetailRow.mark;
                //        }
                //        else
                //        {
                //            orderStockOutDetailRow.mark = "";
                //        }
                //    }

                //    //编辑子表记录,新增
                //    if (detailRow.RowState == DataRowState.Added)
                //    {

                //        orderStockOutDetail.orderStockOutDetailInsert(orderStockOutDetailRow);


                //    }
                //    //修改
                //    if (detailRow.RowState == DataRowState.Modified)
                //    {
                //        orderStockOutDetail.orderStockOutDetailUpdate(orderStockOutDetailRow);
                //    }
                //    //删除
                //    if (detailRow.RowState == DataRowState.Deleted)
                //    {
                //        DataRow delrow = (DataRow)detailRow;
                //        orderStockOutDetail.orderStockOutDetailDel(delrow["recordid", DataRowVersion.Original].ToString());
                //    }


                //}

                tn.Commit();
                return i;

            }
            catch (SqlException ex)
            {
                tn.Rollback();
                if (ex.Number == 2627)
                {
                    throw new Exception("这个入库单号已经存在");
                }


                throw new Exception(ex.Number.ToString() + ex.Message.ToString());

            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());

            }
            finally
            {
                conn.Close();
            }
        }


        /// 删除一行订单成品出库表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
       public int orderStockOutMasterDel(string billNo)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                orderStockOutMasterDAL orderStockOutMaster = new orderStockOutMasterDAL(tn, conn);
                orderStockOutMaster.orderStockOutDel (billNo);
                tn.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());

            }
            finally
            {
                conn.Close();
            }
        }
    }
}
